// pages/withdrawals/withdrawals.js
// pages/balance/balance.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    pageFile: 1,//初始化文档页码
    winHeight: 510,//获取手机屏幕的高度
    hosList: [],//存储列表数据
    listPage: 1,//初始化页面
    imgUrls: [
      {
        "img": "../../images/red-packet/user.jpg", "desc_time": "6秒前提现", 'content': "现金66.6元"
      }, {
        "img": "../../images/red-packet/user.jpg", "desc_time": "6秒前提现", 'content': "现金66.6元"
      }, {
        "img": "../../images/red-packet/user.jpg", "desc_time": "6秒前提现", 'content': "现金66.6元"
      },

    ],//存储轮播假数据
    shareUserList: [],//邀请用户列表
    recommendList: [],//获取文档列表
    enve_id: '',//当前红包ID
    uid: "",//用户id
    playRobots: [],
    title: "",//提示表示
    content: '',//红包内容
    enve_share_info_title: "天天拆红包·领百元现金",
    enve_share_info_border_tips: '可提现',
    successEnve: '0.00',//领取成功弹层显示金额
    successLeftTips: '还需要邀请',//领取成功弹层显示需要人数的提示内容
    successEnveNum: '0',//领取成功需要多少个人
    successRightTips: '个好友领取才可以拆开哦',//领取成功弹层显示需要人数的提示内容

    is_opend:0, //红包是否已经打开 默认为0 表示未打开
    balance:'0.00',//用户已领导的总余额
    head_img:'',
    successGainTips:'恭喜你获得',//用户拆开红包后点击立即领取成功后显示的问你自
    successGainEnve:'0.00',//用户拆开红包后点击立即领取成功后显示的红包金额
    successGainLeftTips:'还需要邀请',//用户红包被拆开后点击领取后的文字
    successGainEnveNum:'0',//用户红包被拆开后点击立即领取显示的拆包人数
    successGainRightTips:'个好友领取才可以拆开哦',//用户红包被拆开后点击立即领取后显示的文字
    shareGainUserList:[],// 用户红包被拆开后点击立即领取后显示的用户列表
    isReceiveRedPacketShow: 0, //是否显示去分享谈层 默认0不显示 1表示显示
    shareGainEnveId:'',//红包拆开后点击立即领取成功后的红包ID 用于分享
    shareGainUid:''//拆开红包点击立即领取成功后的UID 用于分享
  },
  /*返回主页*/
  goHome: function () {
    wx.reLaunch({
      url: '/pages/home/home',
    })
  },
  // 跳转商品详情页
  goDetail: function (e) {
    var _this = this;
    var theGoodsId = e.currentTarget.dataset.goods_id;
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?goodsId=' + theGoodsId,
    })
  },
  // 领取文档
  receiveBtn: function (e) {
    var _this = this;
    var theId = e.currentTarget.dataset.fileid;
    var theHosList = _this.data.hosList;
    var requestData = {
      token: _this.data.token,
      id: theId
    }
    app.post('User/getBooks', requestData, function (res) {
      if (res.code == 2000) {
        console.log(theHosList);
        for (var i = 0; i < theHosList.length; i++) {
          if (theHosList[i].id == theId) {
            theHosList[i].is_show = 1;//1 代表已经领取
          }
          wx.setStorageSync('hosList', theHosList);
        }
        _this.setData({
          hosList: theHosList
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }

    });
    wx.navigateTo({
      url: '../detail/detail?id=' + theId,
    })
  },
  ruleBtn: function () {
    var _this = this;
    _this.setData({
      isResultMaskShow: 1
    });
  },
  closeRuleMask: function () {
    var _this = this;
    _this.setData({
      isResultMaskShow: 0
    });
  },
  // 获取活动规则数据
  getRuleData: function () {
    var _this = this;
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
    };
    app.post('/Enve/rule', requestData, function (res) {
      if(res.code==2000){
      var theRuleList = res.data.list;
      _this.setData({
        ruleList: theRuleList
      })
      }else{
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    })
  },
  // 获取热词文档列表
  getHotRecommend: function () {
    var _this = this;
    var configs = wx.getStorageSync('configs');
    console.log(configs);
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
      type: app.setConfig.type,
      keywords: configs.key_words,
      page: _this.data.page
    }
    app.post('/Goods/recommend', requestData, function (res) {
      if (res.code == 2000) {
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //停止下拉刷新
        var recommendList = _this.data.recommendList;

        recommendList = recommendList.concat(res.data.list);
        _this.setData({
          page: res.data.page,
          recommendList: recommendList
        })
        
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    var configs = wx.getStorageSync('configs');
      _this.setData({
        configs: configs,
        enve_id: options.enve_id
      })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this;
    _this.loop();
    wx.getSystemInfo({
      success: function (res) {
        var theWinHeight = res.windowHeight;
        var theWinWidth = res.windowWidth;
        _this.setData({
          winWidth: theWinWidth,
          winHeight: theWinHeight
        });
      }
    });
  },
  // 判断是否获取到token的值了
  loop: function () {
    var _this = this;
    var theToken = wx.getStorageSync("token");
    if (!theToken) {
      setTimeout(function () { _this.loop() }, 100);
    } else {
      _this.setData({
        token: theToken
      });
      _this.loadConfig();
      _this.enveInfo();
      _this.getRuleData();
      _this.getHotRecommend();
    }
  },
  /**
 * 加载初始化红包配置文件
 */
  loadConfig: function () {
    var _this = this;
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
    };
    app.post('/Enve/enveConfig', requestData, function (res) {
      if (res.code == 2000) {
        console.log(res);
        _this.setData({
          title: res.data.title,
          content: res.data.tips,
          imgUrls: res.data.robot_list,
          playRobots: res.data.robot_list,
          successLeftTips: res.data.enve_success_left_tips,
          successRightTips: res.data.enve_success_right_tips,
          enve_share_info_title: res.data.enve_share_info_title,
          enve_share_info_border_tips: res.data.enve_share_info_border_tips,
          btnIsOk: 1
        });
      } else if (!res.code) {
        wx.showToast({
          title: '服务器去月球了',
          duration: 2000
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  /**检测机器人数据是否播放完毕 */
  animationfinish: function (e) {
    var _this = this;
    if (_this.data.playRobots.length > 0) {
      var playRobots = _this.data.playRobots;
      playRobots.pop();
      _this.setData({
        playRobots: playRobots
      });
    } else {
      //数据播放完毕重新获取数据
      var requestData = {
        token: _this.data.token,
        version: app.setConfig.version,
      };
      app.post('/Enve/getRobots', requestData, function (res) {
        if (res.code == 2000) {
          _this.setData({
            imgUrls: res.data.robot_list,
            playRobots: res.data.robot_list
          });
        } else if (!res.code) {
          wx.showToast({
            title: '服务器去月球了',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 2000
          });
        }
      });
    }
  },
  /**
   * 
   * 获取红包详情
   */
  enveInfo: function () {
    var _this = this;
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
      enve_id: _this.data.enve_id,
    };
    app.post('/Enve/enveInfo', requestData, function (res) {
      if (res.code == 2000) {
        _this.setData({
          shareUserList: res.data.share_user_list,
          successEnveNum: res.data.surplus_share_num,
          successEnve: res.data.enve_amount,
          balance: res.data.balance, 
          is_opend: res.data.is_opend,
          uid:res.data.user_id
        });
      } else if (!res.code) {
        wx.showToast({
          title: '服务器去月球了',
          duration: 2000
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    });
  },

  //申请提现
  goWithdrawals:function(){
    var _this = this;
    if(_this.data.is_opend==0){
        //红包未打开
        _this.setData({
          userListBox:1
        });
    }else{
       //红包已经打开
      var requestData = {
        token: _this.data.token,
        version: app.setConfig.version,
        enve_id: _this.data.enve_id,
      };
      app.post('/Enve/goWithdraw', requestData, function (res) {
               if(res.code==2000){
                    wx.navigateTo({
                      url: '../balance/balance?gainSuccess=1',
                    })
               }else{
                 wx.showToast({
                   title: res.msg,
                   icon: "none",
                   duration: 2000
                 });
               }
      })
    }
  },
  goShare:function(){
    var _this = this;
    if (_this.data.enve_id) {
      wx.redirectTo({
        url: '../share-red-packet/share-red-packet?enve_id=' + _this.data.enve_id  + "&uid=" + _this.data.uid,
      });
    } else {
      wx.showToast({
        title: '无法获取红包信息',
        duration: 2000
      });
    }
  },
  /**红包打开后重新领取分享按钮 */
  goGainShareEvent:function(){
    var _this = this;
    if (_this.data.shareGainEnveId) {
      wx.navigateTo({
        url: '../share-red-packet/share-red-packet?enve_id=' + _this.data.shareGainEnveId + "&uid=" + _this.data.shareGainUid,
      });
    } else {
      wx.showToast({
        title: '无法获取红包信息',
        duration: 2000
      });
    }
  },
  /*立即领取红包*/
  saveEnveEvent:function(e){
    var _this = this;
      var requestData = {
        token: _this.data.token,
        version: app.setConfig.version,
      };
      app.post('/Enve/createEnve', requestData, function (res) {
        if (res.code == 2000) {
          _this.setData({
            successGainTips: res.data.enve_success_tips,
            successGainEnve: res.data.enve_amount,
            successGainLeftTips: res.data.enve_success_left_tips,
            successGainEnveNum: res.data.splus_num,
            successGainRightTips: res.data.enve_success_right_tips,
            shareGainUserList: res.data.share_user_list,
            isReceiveRedPacketShow: 1,
            shareGainEnveId: res.data.enve_id,
            shareGainUid: res.data.uid,
          });
        } else if (!res.code) {
          wx.showToast({
            title: '服务器去月球了',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 2000
          });
        }
      });
  },
  /*下拉刷新事件*/
  onPullDownRefresh:function(){
      this.enveInfo();
      wx.stopPullDownRefresh();
  },
  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {
    var _this = this;
    _this.getHotRecommend();
  },
  /**关闭closeUserListBoxEvent 弹层 */
  closeUserListBoxEvent:function(){
    var _this = this;
    _this.setData({
      userListBox:0
    })
  }
})
